package junior.db.dao.mybatis.base;

import junior.db.dao.base.IBaseId;
import junior.db.dao.update.BatchInsertOption;
import junior.db.dao.update.BatchUpsertOption;
import junior.db.dao.update.InsertOption;
import junior.db.dao.update.UpdateMapperProvider;
import junior.db.dao.update.UpsertOption;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author wubo
 * @date 2024/8/27 16:05
 */
public interface BaseInsertMapper<E extends IBaseId, ID> extends IBaseMapper<E> {
    
    /**
     * 批量插入
     * @param entity
     * @param option
     * @return
     */
    @InsertProvider(type = UpdateMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys=true, keyColumn="id", keyProperty="entity.id")
    int insert(@Param("entity") E entity, @Param("option") InsertOption option);
    
    /**
     * <h3>批量更新</h3>
     * @param entities
     * @param option
     * @return 影响记录数
     */
    @InsertProvider(type=UpdateMapperProvider.class, method="batchInsert")
    @Options(useGeneratedKeys=true, keyColumn="id", keyProperty="entities.id")
    int batchInsert(@Param("entities") List<E> entities, @Param("option") BatchInsertOption option);
    
    /**
     * <h3>插入或更新，如果是自增主键，将会设置到entity.id</h3>
     * @param entity
     * @param option
     * @return 影响记录数
     */
    @InsertProvider(type=UpdateMapperProvider.class, method="upsert")
    @Options(useGeneratedKeys=true, keyColumn="id", keyProperty="entity.id")
    int upsert(@Param("entity") E entity, @Param("option") UpsertOption option);
    
    /**
     * <h3>批量插入或更新</h3>
     * 注意：此方法不能返回自增的主键
     * @param entities
     * @param option
     * @return 影响记录数
     */
    @InsertProvider(type=UpdateMapperProvider.class, method="batchUpsert")
    int batchUpsert(@Param("entities") List<E> entities, @Param("option") BatchUpsertOption option);
}
